﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Transactions;
using System.ComponentModel;

namespace Locations
{
    /// <summary>
    /// Interaction logic for Location.xaml
    /// </summary>
    public partial class Location : Window
    {
        private SalesManagerEntities _context = new SalesManagerEntities();
        //private LocationDataContext _context = new LocationDataContext(Location.Properties.Settings.Default.SalesManagerConnectionString);
        private CollectionViewSource _locationViewSource;
        private CollectionViewSource _contactsViewSource;
        //private CollectionViewSource _contactViewSource;
        private string _locationTypeCode;

        public Location()
        {
            InitializeComponent();
            controlAddress.SetContext(_context);
            controlContactPoints.SetContext(_context);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _locationTypeCode = App.Args[0];
            Title = App.Args[1];

            _locationViewSource = (CollectionViewSource)FindResource("Location");
            _contactsViewSource = (CollectionViewSource)FindResource("Contacts");
            //_contactViewSource = (CollectionViewSource)FindResource("Contact");

            _locationViewSource.Source = from c in _context.tblLocation.Include("Address").Include("Contacts").Include("Contacts.Person").Include("Contacts.Person.ContactPoints")
                                         where c.LocationType.LocationTypeCode == _locationTypeCode
                                            && c.Active
                                         select c;

            _locationViewSource.View.CurrentChanged += new EventHandler(LocationView_CurrentChanged);

            CollectionViewSource languageViewSource = (CollectionViewSource)FindResource("Language");
            languageViewSource.Source = from c in _context.codeLanguage
                                        orderby c.Name
                                        select c;

            CollectionViewSource parentLocationViewSource = (CollectionViewSource)FindResource("ParentLocation");
            List<string> parentTypeCodes = new List<string>(new string[] { "O", _locationTypeCode });
            parentLocationViewSource.Source = from c in _context.tblLocation
                                              where (c.LocationType.LocationTypeCode == "O" || c.LocationType.LocationTypeCode == _locationTypeCode)
                                                    && c.Active
                                              orderby c.ShortName
                                              select c;
        }

        void LocationView_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void btnAddContact_Click(object sender, RoutedEventArgs e)
        {
            if (_locationViewSource.View.CurrentItem != null)
            {
                tblLocationPerson newContact = new tblLocationPerson();
                newContact.Location = (tblLocation)listViewLocations.SelectedItem;
                tblPerson newPerson = new tblPerson();
                newContact.Person = newPerson;
                _context.AddTotblLocationPerson(newContact);
                _contactsViewSource.View.MoveCurrentTo(newContact);
                textTitle.Focus();
            }
        }

        private void btnRemoveContact_Click(object sender, RoutedEventArgs e)
        {
            ((IEditableCollectionView)_contactsViewSource.View).Remove(_contactsViewSource.View.CurrentItem);
        }

        private static int _foo = 0;
        private void btnAddLocation_Click(object sender, RoutedEventArgs e)
        {
            tblLocation newLocation = (tblLocation)((IEditableCollectionView)_locationViewSource.View).AddNew();
            newLocation.LocationType = _context.codeLocationType.First(f => f.LocationTypeCode == _locationTypeCode);
            newLocation.ShortName = "New " + Title;

            newLocation.Address = new tblAddress();
            newLocation.Address.Country = (codeCountry)_context.GetObjectByKey(new System.Data.EntityKey("SalesManagerEntities.codeCountry", "CountryID", 1));

            ((IEditableCollectionView)_locationViewSource.View).CommitNew();
            _locationViewSource.View.MoveCurrentTo(newLocation);

            textShortName.Focus();
            textShortName.SelectAll();
        }

        private void btnDeleteLocation_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    _context.SaveChanges();
                    ts.Complete();
                    Close();
                }
                catch (Exception err)
                {
                    MessageBox.Show("Error saving changes\n" +
                        err.Message, "Data Error");
                }
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void controlContactPoints_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
